package com.bw.controller;

import com.alibaba.fastjson2.JSON;
import com.bw.domain.Role;
import com.bw.domain.TbMenu;
import com.bw.domain.User;
import com.bw.service.UserService;
import com.bw.utils.CheckUtil;
import com.bw.utils.MenuUtil;
import com.bw.utils.R;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Wrj
 * @Date: 2025/3/8 08:31
 * @Description:
 **/
@Controller
@RequestMapping("auth")
public class AuthController {
    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 跳转登录界面
     * @return
     */
    @RequestMapping("tologin")
    public String tologin(){
        return "login";
    }




    /**
     * 执行登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    //允许密码错误的总次数
    int totalnum=5;
    @ResponseBody
    @RequestMapping("login")
    public R login(String username, String password, HttpSession session){


        //登录失败的key
        String loginFailCount="loginFailCount"+username;
        //登录失败次数过多，锁定账户的key
        String lockFailCount="lockFailCount"+username;

        Object o = redisTemplate.opsForValue().get(lockFailCount);
        if (o!=null){
            return R.ERROR(500,"账号已经锁定");
        }


        if (username!=null && username.length()<8){
            return R.ERROR(404,"账号不能为空且不能小于8位");
        }
        if (password!=null && password.length()<6){
            return R.ERROR(404,"密码长度不能为空且不能小于6位");
        }
        if (!CheckUtil.mustNumAndLetter(password)){
            return R.ERROR(404,"密码必须包含字母和数字");
        }


        User user=userService.login(username);
        if (user==null){
            return R.ERROR(404,"该账号未注册");
        }
        if (!user.getPassword().equals(password)){

            //让登录失败次数加1
            Long increment = redisTemplate.opsForValue().increment(loginFailCount);
            //如果次数大于总次数
            if (increment>=totalnum){

                //锁定5分钟
                redisTemplate.opsForValue().set(lockFailCount,1,5,TimeUnit.MINUTES);
                //删除登录失败的次数
                redisTemplate.delete(loginFailCount);
                return R.ERROR(500,"账号已经锁定5分钟");
            }

            return R.ERROR(404,"密码错误还有"+(totalnum-increment)+"次");
        }
        session.setAttribute("user",user);

        redisTemplate.opsForValue().set("user"+user.getUserId(),user);

        List<Role> roleList=userService.listRole(user.getUserId());

        redisTemplate.opsForValue().set("role"+user.getUserId(),roleList);

        List<TbMenu> tbMenuList=userService.listMenus(user.getUserId());
        //转成树形结构
        List<TbMenu> menuList = MenuUtil.treeMenus(tbMenuList, null);


        redisTemplate.opsForValue().set("menu"+user.getUserId(),menuList);

        return R.OK();
    }
}
